#include "stm32f4xx_hal.h"
#include "stm32f4xx_ll_rcc.h"
#include "stdio.h"
#include "log.h"
#include "usbd_conf.h"

#include "DevicesADC.h"
#include "DevicesDelay.h"
#include "DevicesQueue.h"
#include "DevicesMem.h"
#include "DevicesTime.h"
#include "DevicesTimer.h"
#include "DevicesSPIFlash.h"
#include "DevicesSoftTimer.h"
#include "DevicesKey.h"
#include "DevicesWatchDog.h"
#include "DevicesRandom.h"
#include "DevicesCRC.h"
#include "DevicesUart.h"
#include "DevicesPower.h"

#include "DriverVoltage.h"
#include "DriverCurrent.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

#include "taskSoftTimer.h"
#include "taskKey.h"
#include "taskMessageRead.h"
#include "taskVoltage.h"
#include "taskCurrent.h"
#include "taskSystem.h"


/* 系统任务句柄 */
TaskHandle_t g_TaskSystemHand = NULL;


void vSystemHardInit(void)
{
    vDelayInit();

    vHardCRCInit();

    vRandHardInit();

    vMemInit();
    enumQueueInit();

    vPowerInit();

    vADC1Init();

    vVoltageInit();

    vCurrentInit();

    vKeyInit();

    vUart1Init();
    vUart6Init();

    vSPIFlashInit();

    vUSBDeviceInit();

    /* 用作时基 */
    cTimer6Init();

    vWatchdogInit();
}

void vTaskSystem(void *pvParameters)
{
    printfSystem("FreeRTOS start runing...\r\n");


    xTaskCreate(vTaskCurrent,            "Current",            1024, NULL, configMAX_PRIORITIES - 2, &g_TaskCurrentHand);

    xTaskCreate(vTaskVoltage,            "Voltage",            1024, NULL, configMAX_PRIORITIES - 3, &g_TaskVoltageHand);

    xTaskCreate(vTaskMessageRead,        "Message Read",       1024, NULL, configMAX_PRIORITIES - 7, &g_TaskMessageReadHand);

    xTaskCreate(vTaskKey,                "Key",                512,  NULL, configMAX_PRIORITIES - 9, &g_TaskKeyHand);


    vSystemHardInit();


    vTaskSoftTimerInit();

    printfSystem("SystemInit succeed.\r\n");


    while(1)
    {
        vWatchdogReload();

        vTaskDelay(100 / portTICK_RATE_MS);
    }


    // /* 删除自己 */
    // vTaskDelete( NULL );
}
